package com.chunwei.advert.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @program: our-task
 * @description: 对数据库每条记录的创建时间和更新时间自动进行填充
 * @author: 王个球
 * @create: 2021/11/16 19:10
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
  /**
   * 插入时的填充策略
   *
   * @param metaObject
   */
  @Override
  public void insertFill(MetaObject metaObject) {
    this.setFieldValByName("createTime", new Date(), metaObject);
    this.setFieldValByName("updateTime", new Date(), metaObject);
    this.setFieldValByName("addDate", new Date(), metaObject);
    this.setFieldValByName("lastModifyDate", new Date(), metaObject);
    this.setFieldValByName("inputTime", new Date(), metaObject);
    this.setFieldValByName("borrowingTime", new Date(), metaObject);
  }

  /**
   * 更新时的填充策略
   *
   * @param metaObject
   */
  @Override
  public void updateFill(MetaObject metaObject) {
    this.setFieldValByName("updateTime", new Date(), metaObject);
    this.setFieldValByName("lastModifyDate", new Date(), metaObject);
  }
}